package Wormish.agiletre.se.ParticleSystem;

import java.util.LinkedList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import Wormish.agiletre.se.Math.MathHelper;
import Wormish.agiletre.se.Math.Vec2f;

public class ParticleSystem
{
	LinkedList<Particle> Particles;
	LinkedList<Particle> ActiveParticles;
	
	Bitmap ParticleTexture;
	int numberOfParticles;
	int maxNumberOfParticles;
	public boolean RandomVelocity = true;
	public Vec2f SpawnVelocity = Vec2f.Zero;
	
	public static Bitmap ExplosionTexture;
	public static Bitmap BloodTexture;
	
	public float MinParticleSize = 0.1f;
	public float MaxParticleSize = 0.25f;
	
	public float MinParticleLifeSpan = 0.1f;
	public float MaxParticleLifeSpan = 1f;
	
	public float ParticleVelocity = 5.0f;
	public float ParticleDistribution = 10f;
	public boolean IsAlive = true;
	
	public ParticleSystem(int maxNumberOfParticles, Bitmap texture)
	{
		this.maxNumberOfParticles = maxNumberOfParticles;
		Particles = new LinkedList<Particle>();
		ActiveParticles = new LinkedList<Particle>();
		
		for(int i = 0; i < maxNumberOfParticles; i++)
		{
			Particles.add(new Particle());
		}
		
		ParticleTexture = texture;
	}
	
	public static void LoadContent(Context theContext)
	{
		if(ExplosionTexture == null)
		{
			ExplosionTexture = BitmapFactory.decodeResource(theContext.getResources(), Wormish.agiletre.se.R.drawable.explosion);
		}
		if(BloodTexture == null)
		{
			BloodTexture = BitmapFactory.decodeResource(theContext.getResources(), Wormish.agiletre.se.R.drawable.blood);
		}
	}
	
	public void InitializeParticles(Vec2f spawnPosition, int numberOfParticles)
	{
		for(int i = 0; i < numberOfParticles || i < maxNumberOfParticles; i++)
		{
			Particle temp;
			
			if(Particles.size() > 0)
			{
				temp = Particles.remove();
			}
			else
			{
				temp = ActiveParticles.remove();
			}

			temp.Rotation = MathHelper.Random(360);
			temp.Scale = MathHelper.Random(MinParticleSize, MaxParticleSize);
			temp.Position = spawnPosition.add(new Vec2f(ParticleDistribution * MathHelper.Random(-1f, 1f), ParticleDistribution * MathHelper.Random(-1f, 1f)), true);
			temp.LifeSpan = MathHelper.Random(MinParticleLifeSpan, MaxParticleLifeSpan);
			if(RandomVelocity)
			{
				temp.Velocity = new Vec2f(ParticleVelocity * MathHelper.Random(-1f, 1f), ParticleVelocity * MathHelper.Random(-1f, 1f));
			}
			else
			{
				temp.Velocity = SpawnVelocity;
			}
			temp.Texture = ParticleTexture;
			ActiveParticles.add(temp);
		}
	}
	
	public void Draw(Canvas canvas)
	{
		for(int i = 0; i < ActiveParticles.size(); i++)
		{
			ActiveParticles.get(i).Draw(canvas);
		}
	}
	
	public boolean Update(float dt)
	{
		for(int i = 0; i < ActiveParticles.size(); i++)
		{
			ActiveParticles.get(i).Update(dt);
			if(!ActiveParticles.get(i).IsActive)
			{
				Particles.add(ActiveParticles.remove(i));
			}
		}

		return true;
	}
}
